using System;
using System.Windows.Forms;
using System.Diagnostics;
using TAAlert.Alerts;

namespace TAAlert.GUI
{
    public partial class CreateNewAlertGUI : Form
    {
        private Alert alertM = null;

        public Alert Alert
        {
            get { return alertM; }
        }

        /// <summary>Get all prameters from user and create a new alert. Store itin property Alert</summary>
        /// <param name="strategyName">StartegyName to create</param>
        public CreateNewAlertGUI(StrategyName strategyName)
        {
            InitializeComponent();
            // Add Alert Types
            foreach (string alertName in Enum.GetNames(typeof(TAAlert.StrategyName)))
                this.cbAlertTypeM.Items.Add(alertName);
            this.cbAlertTypeM.SelectedItem = strategyName.ToString();

            // Add available loaders
            foreach (string loaderName in Enum.GetNames(typeof(TALoaders.Loader)))
                this.cbLoadersM.Items.Add(loaderName);
            this.cbLoadersM.SelectedIndex = (int)TALoaders.Loader.Yahoo;
            // See also this.cbAlertTypeM_SelectedIndexChanged since it is called on IndexSelection
            this.Text = "Create New " + strategyName.ToString() + " Alert";
        }

        private void quit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btCreate_Click(object sender, EventArgs e)
        {
            TALoaders.BaseLoader loader = TALoaders.DownloaderFactory.createCachedDownloader(this.cbLoadersM.SelectedItem.ToString());
            string ticker = cbTickersM.Text;
            int nShort = (int) nmNEmaShortM.Value;
            int nLong = (int)nmNEmaLongM.Value;
            double volT = Convert.ToDouble(txtFastM.Text);
            int nFast = (int)volT;
 
            StrategyName strategy = (StrategyName)Enum.Parse(typeof(StrategyName), cbAlertTypeM.SelectedItem.ToString());
            switch (strategy)
            {
                case StrategyName.TwoEMA:
                    alertM = new TwoEmaAlert(loader, ticker, nShort, nLong, volT);
                    break;
                case StrategyName.EMA:
                    alertM = new EMAAlert(loader, ticker, nShort);
                    break;
                case StrategyName.MACD:
                    alertM = new MACDAlert(loader, ticker, nShort, nLong, nFast);
                    break;
                case StrategyName.VEMA:
                    alertM = new VEMAAlert(loader, ticker, nShort, nLong, volT);
                    break;
                default:
                    throw new Exception("Unsupported Alert type " + cbAlertTypeM.SelectedItem.ToString());
            }

            this.quit_Click(sender, e);
        }

        private void cbAlertTypeM_SelectedIndexChanged(object sender, EventArgs e)
        {
            StrategyName strategy = (StrategyName)Enum.Parse(typeof(StrategyName), cbAlertTypeM.SelectedItem.ToString());
            switch (strategy)
            {
                case StrategyName.EMA:
                    lbnEMAShortM.Text = "nEMA:";
                    nmNEmaLongM.Visible = false;
                    lbnEMALongM.Visible = false;
                    txtFastM.Visible = false;
                    lbnFastM.Visible = false;
                    createButtonM.Text = "CREATE EMA Alert";
                    break;
                case StrategyName.TwoEMA:
                    lbnEMAShortM.Text = "nShort:";
                    lbnEMALongM.Text = "nLong :";
                    nmNEmaLongM.Visible = true;
                    lbnEMALongM.Visible = true;
                    txtFastM.Visible = true;
                    txtFastM.Text = Convert.ToString(0.30);
                    lbnFastM.Text = "volT:";
                    lbnFastM.Visible = true;
                    createButtonM.Text = "CREATE TwoEMA Alert";
                    break;
                 case StrategyName.MACD:
                    lbnEMAShortM.Text = "nShort:";
                    lbnEMALongM.Text = "nLong :";
                    nmNEmaLongM.Visible = true;
                    lbnEMALongM.Visible = true;
                    txtFastM.Visible = false;
                    lbnFastM.Visible = true;
                    createButtonM.Text = "CREATE MACD Alert";
                    break;
                case StrategyName.VEMA:
                    lbnEMAShortM.Text = "nLowVol:";
                    lbnEMALongM.Text = "nHighVol:";
                    lbnFastM.Text = "volT:";
                    nmNEmaLongM.Visible = true;
                    lbnEMALongM.Visible = true;
                    txtFastM.Visible = true;
                    txtFastM.Text =  Convert.ToString(0.30);
                    lbnFastM.Visible = true;
                    createButtonM.Text = "CREATE VEMA Alert";
                    break;
                default:
                    throw new Exception("Unsupported Alert type " + cbAlertTypeM.SelectedItem.ToString());
            }
        }

        private void cbLoadersM_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbTickersM.Items.Clear();
            string[] tickers = TALoaders.DownloaderFactory.getDefaultTickers(this.cbLoadersM.SelectedItem.ToString());

            foreach (string ticker in tickers)
                this.cbTickersM.Items.Add(ticker);
            this.cbTickersM.SelectedIndex = 0;
        }
    }
}